@extends('admin.tableLayout')
@section('title','文章列表')
@section('barName','文章列表')
@section('quote')
    <button type="button" class="layui-btn layui-btn-small" id="getAll" onclick="javascript:location.href='{{route('admin.posts-add')}}'"><i class="fa fa-plus" aria-hidden="true"></i> 添加文章</button>
    <hr/>
   <form class="layui-form" >
        <label>所属栏目：</label>
        <div class="layui-inline">
            <select name="category_id"  id="category_id" lay-search >
                <option value="">全部</option>
                {!! $categories !!}
            </select>

        </div>
        <label>状态：</label>
        <div class="layui-inline">
            <select name="status"  id="status" lay-search lay-filter="select">
                <option value="">全部</option>
                @foreach($status as $k=>$v)
                    <option value="{{$k}}">{{$v}}</option>
                @endforeach
            </select>

        </div>
       <label>属性：</label>
       <div class="layui-inline">
           <select name="article_attr"  id="article_attr" lay-search lay-filter="select">
               <option value="">全部</option>
               @foreach($article_attrs as $k=>$v)
                   <option value="{{$k}}">{{$v}}</option>
               @endforeach
           </select>

       </div>
        <div id="select" class="layui-inline"></div>

        <div class="layui-inline">
            <input type="text" class="layui-input" width="200px" id="datetime" name="searchTime" value="" placeholder="日期范围搜索">
        </div>
        <div class="layui-inline">
            <input type="text" name="searchKey" placeholder="文章名称" autocomplete="off" value="" class="layui-input">
        </div>
        <button lay-filter="search" class="layui-btn" id="search"><i class="fa fa-search" aria-hidden="true"></i> 查询</button>
    </form>
@endsection

@section('checkbox_btn')
    @can('posts')
    <button class="layui-btn" data-type="draftAll">批量将选中文章移至草稿</button>
    <button class="layui-btn" data-type="publishedAll">批量发布选中文章</button>
    <button class="layui-btn" data-type="deleteAll">批量删除选中文章</button>
    {{--<button class="layui-btn" data-type="pushAll">将选中文章发布至微信</button>--}}
    @endcan
@endsection
@section('toolbar')
    @can('posts')
        <a class="layui-btn layui-btn-xs" lay-event="see">查看</a>

        <a class="layui-btn layui-btn-xs " lay-event="edit">编辑</a>
    @endcan
    @can('posts')

        @{{#  if(d.status == '发布'){ }}
    <a class="layui-btn layui-btn-xs" lay-event="draft">{{$postManager->getStatusString(\App\Post::DRAFT)}}</a>
    @{{#  } else { }}
    <a class="layui-btn layui-btn-xs" lay-event="published">{{$postManager->getStatusString(\App\Post::PUBLISHED)}}</a>
    @{{#  } }}
@endcan
    @can('posts')
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete">删除</a>
    @endcan
@endsection
@section('tableRender')
    <script id="article_attr_placeholder" type="text/html">
        @{{# for(var article_attr in d.article_attrAry){ }}
            @{{# if(article_attr=='f'){ }}
                    <span class="layui-badge">轮播</span>
            @{{# } }}
        @{{# } }}
    </script>
    <script>
        layui.use(['table','jquery','form'], function(){
            var table = layui.table;
            var $=layui.jquery;
            var form=layui.form;
            var category_id=$("#category_id").val();
            var _renderLoad = layer.msg('正在查找文章，请稍候',{icon: 16,time:false,shade:0.8});
            ;
            if(category_id!=''){
                $.get('/admin/writing/get_theme',{category_id:category_id},function (data) {
                    if(data!=''){
                        $("#select").html(data);
                        form.render('select');
                    }else{
                        $("#select").html('');
                    }
                })
            }
            var render=table.render({
                elem:"#table",
                page:true,
                id:'idTest',
                url:'{{route('admin.postsAjax')}}',
                where:{
                    searchKey:$("input[name='searchKey']").val(),
                    searchTime:$("input[name='searchTime']").val(),
                    category_id:$("#category_id").val(),
                    area_id:$("#area").val(),
                    status:$("#status").val(),
                    article_attr:$('#article_attr').val()
                },
                height:400,
                cols:  [[ //标题栏
                    {checkbox:true,fixed:true},
                    {field: 'id', title: 'ID', width: 80,fixed:true}
                    ,{field: 'post_title', title: '标题', width: 200},
                    {field: 'category_id', title: '栏目', width: 80},
                    {field: 'status', title: '状态', width: 100},
                    {field: 'article_attrAry', title: '属性', width: 100,templet:"#article_attr_placeholder"},
                    {field: 'created_at', title: '创建时间', width: 150},
                    {field: 'updated_at', title: '修改时间', width: 150},
                    {fixed: 'right', width:350,title:'操作', align:'center', toolbar: '#toolbar'}
                    //这里的toolbar值是模板元素的选择器
                ]],
                response: {//返回格式自定义
                    statusName: 'code' //数据状态的字段名称，默认：code
                    ,statusCode: 0 //成功的状态码，默认：0
                    ,msgName: 'msg' //状态信息的字段名称，默认：msg
                    ,countName: 'total' //数据总数的字段名称，默认：count
                    ,dataName: 'data' //数据列表的字段名称，默认：data
                },
                done:function () {
                    layer.close(_renderLoad);
                }
            });
            $('#search').on('click',function () {
                var data = {
                    searchKey:$("input[name='searchKey']").val(),
                    searchTime:$("input[name='searchTime']").val(),
                    category_id:$("#category_id").val(),
                    area_id:$("#area").val(),
                    status:$("#status").val(),
                    article_attr:$('#article_attr').val()
                };
                _renderLoad = layer.msg('正在查找文章，请稍候',{icon: 16,time:false,shade:0.8});
                render.reload(
                    {
                        where:data
                    }
                );
                return false;
            })
            table.on('checkbox(table)', function(obj){
                //console.log(obj)
            });


            //上方
            var   active = {
                deleteAll: function(){ //获取选中数据
                    var checkStatus = table.checkStatus('idTest')
                        ,data = checkStatus.data;
                    var str=[];
                    if(data==''){
                        layer.msg('请选中所删除的行',{icon:5})
                        return false;
                    }
                    for(var i=0;i<data.length;i++){
                        str.push(data[i].id);
                    }

                    layer.confirm('是否删除所选',function(index) {

                        $.get('{{route('admin.posts',['act'=>'delete'])}}', {id: str,type:1}, function (obj) {
                            if (obj.info == 'success') {
                                layer.msg('操作成功', {icon: 1});
                                render.reload();
                            } else {
                                layer.msg('网络繁忙', {icon: 5});
                            }

                        })
                    })

                },
                draftAll: function(){ //获取选中数目
                    var checkStatus = table.checkStatus('idTest')
                        ,data = checkStatus.data;
                    var str=[];
                    if(data==''){
                        layer.msg('请选中所要显示的行',{icon:5})
                        return false;
                    }
                    for(var i=0;i<data.length;i++){
                        str.push(data[i].id);
                    }
                    layer.confirm('是否将选中的文章变为草稿',function(index){
                        layer.close(index);
                        //服务端发送审核指令
                        $.get('{{route('admin.posts',['act'=>'draft'])}}',{id:str,type:1},function(obj){
                            if(obj.info=='success'){
                                layer.msg('操作成功',{icon:1});
                                render.reload();
                            }else{
                                layer.msg('网络繁忙',{icon:5});
                            }

                        })
                    });

                }
                ,
                publishedAll: function(){ //获取选中数目

                    var checkStatus = table.checkStatus('idTest')
                        ,data = checkStatus.data;
                    var str=[];
                    if(data==''){
                        layer.msg('请选中所发布的行',{icon:5})
                        return false;
                    }
                    for(var i=0;i<data.length;i++){
                        str.push(data[i].id);
                    }
                    layer.confirm('是否发布选中文章',function(index){
                        layer.close(index);
                        //服务端发送审核指令
                        $.get('{{route('admin.posts',['act'=>'published'])}}',{id:str,type:1},function(obj){
                            if(obj.info=='success'){
                                layer.msg('操作成功',{icon:1});
                                render.reload();
                            }else{
                                layer.msg('网络繁忙',{icon:5});
                            }

                        })
                    });

                },
                pushAll:function () {
                    var checkStatus = table.checkStatus('idTest')
                        ,data = checkStatus.data;
                    var str=[];
                    if(data==''){
                        layer.msg('请选中所推送的行',{icon:5})
                        return false;
                    }
                    for(var i=0;i<data.length;i++){
                        str.push(data[i].id);
                    }
                    //弹窗选择封面
                    layer.confirm('是否发推送选中的文章',function(index){
                        layer.close(index);
                        //服务端发送审核指令
                        $.get('{{route('admin.posts',['act'=>'push'])}}',{id:str},function(obj){
                            if(obj.info=='success'){
                                layer.msg('正在研发中......',{icon:1});
                                render.reload();
                            }else{
                                layer.msg('网络繁忙',{icon:5});
                            }

                        })
                    });
                }
                ,isAll: function(){ //验证是否全选
                    var checkStatus = table.checkStatus('idTest');
                    layer.msg(checkStatus.isAll ? '全选': '未全选')
                }
            };
            $('.demoTable .layui-btn').on('click', function(){
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });
            table.on('tool(table)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
                var data = obj.data; //获得当前行数据
                var layEvent = obj.event; //获得 lay-event 对应的值
                var tr = obj.tr; //获得当前行 tr 的DOM对象
                 if(layEvent === 'delete'){ //删除
                    layer.confirm('真的删除行么', function(index){
                        layer.close(index);
                        //向服务端发送删除指令
                        $.get('{{route('admin.posts',['act'=>'delete'])}}',{id:data.id},function(e){
                            if(e.info=='success'){
                                layer.msg('操作成功',{icon:1});
                                obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                            }
                        });
                    });
                } else if(layEvent === 'edit'){ //编辑
                     location.href='/admin/posts/edit/'+data.id;
                    //do something
                    //同步更新缓存对应的值
                }else if(layEvent === 'draft'){
                    layer.confirm('是否将文章作为草稿',function(index){
                        layer.close(index);
                        //服务端发送审核指令
                        $.get('{{route('admin.posts',['act'=>'draft'])}}',{id:data.id},function(e){
                            if(e.info=='success'){
                                layer.msg('操作成功',{icon:1});
                                render.reload();
                            }else{
                                layer.msg('网络繁忙',{icon:5});
                            }

                        })
                    });

                }else if(layEvent=='published'){
                    layer.confirm('是否发布文章',function(index){
                        layer.close(index);
                        //服务端发送审核指令
                        $.get('{{route('admin.posts',['act'=>'published'])}}',{id:data.id},function(e){
                            if(e.info=='success'){
                                layer.msg('操作成功',{icon:1});
                                render.reload();
                            }else{
                                layer.msg('网络繁忙',{icon:5});
                            }

                        })
                    });
                }else if(layEvent == 'see'){
                     window.open('/wap/'+data.id+'/page');
                 }else if(layEvent == 'wechat'){
                    location.href="{{route('admin.wechat.send-add')}}?id="+data.id;
                 }
            });
        });
    </script>
@endsection

